﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ProCaiGouRule
    {

        #region 导出
        /// <summary>
        /// 用户列表（导出Excel）
        /// </summary>
        /// <returns></returns>
        public static async Task<MemoryStream> GetExportList(List<string> ids, List<ColumnModel> cols)
        {
            //List<string> ids = new List<string>();
            //List<ColumnModel> cols = new List<ColumnModel>();

            //取出数据源
            DataTable exportTable = await GetExportList(ids);
            //设置导出格式
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.Title = "招标申请列表";
            excelconfig.TitleFont = "微软雅黑";
            excelconfig.TitlePoint = 25;
            //excelconfig.HeadPoint = 12;
            excelconfig.FileName = "招标申请列表.xls";
            excelconfig.IsAllSizeColumn = true;
            //每一列的设置,没有设置的列信息，系统将按datatable中的列名导出
            excelconfig.ColumnEntity = new List<ColumnModel>();
            for (int i = 0; i < cols.Count; i++)
            {
                excelconfig.ColumnEntity.Add(new ColumnModel() { Column = cols[i].Column, ExcelColumn = cols[i].ExcelColumn });
            }

            return ExcelHelper.ExportMemoryStream(exportTable, excelconfig);
        }

        public async static Task<DataTable> GetExportList(List<string> ids)
        {
            //throw new NotImplementedException();
            string yids = string.Join("','", ids);
            yids = "'" + yids + "'";
            string strsql = "select * from View_ProZBCGLists where id in (" + yids + ")";
            return await new RepositoryFactory().BaseRepository().FindTable(strsql);
        }

        #endregion

        #region 采购
        //查询列表
        public static async Task<(IEnumerable<dynamic>, int)> GetCaiGouIndexList(ProCaiGouQuerry querry, string strdepids)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.Jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.Jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.Xmmc)))
            {
                str = str + " and xmmc like '%" + querry.Xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.cgmc)))
            {
                str = str + " and cgmc like '%" + querry.cgmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.cgfs)))
            {
                str = str + " and cgfs like '%" + querry.cgfs + "%'";
            }


            if (!(string.IsNullOrEmpty(querry.Xmfl)))
            {
                str = str + " and xmflname = '" + querry.Xmfl + "'";
            }
            if (!(string.IsNullOrEmpty(querry.Begindate.ToString())))
            {
                str = str + " and jhkssj >= ''" + querry.Begindate + "'' ";

               
            }

            if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
            {
                str = str + " and jhkssj <= ''" + querry.Enddate + "''";
            }

            //else
            //{
            //    if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
            //    {
            //        str = str + " and (jhkssj <= ''" + querry.Begindate + "'' or jhwcsj <= ''" + querry.Enddate + "'' )";
            //    }
            //}

            if (!(string.IsNullOrEmpty(querry.Status)))
            {
                str = str + " and spstatus = ''"+ querry.Status + "''";
            }

            string sql = "select * from View_ProZBCGLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");//await UserRule.GetUserDepid(uid)
            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public async static Task<dynamic> GetXMCGEntity(string cgid)
        {
            string strsql = "select * from View_ProZBCGLists where id = '" + cgid + "'";
            var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

        

            if (null != CProZBLS)
            {
                //采购 明细
                strsql = "select * from CProZBCG where zbid = '" + cgid + "'";
                var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBCG>(strsql);
                CProZBLS.cgmx = CProZBCGmxs;

                //单项信息
                string dxid = CProZBLS.dxid;
                var CProdxxxs = await ProLiXiangRule.GetBDById(dxid);
                CProZBLS.dxxx = CProdxxxs; 
            }
            //dynamic rest = new ExpandoObject();
            //rest.cgxx = CProZBLS;
            //rest.cgmx = CProZBCGmxs;
            return CProZBLS;
        }

        public async static Task<dynamic> GetXMCGEntityByDxid(string dxid)
        {
            string strsql = "select * from View_ProZBCGLists where dxid = '" + dxid + "'";
            var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

            strsql = "select * from CProZBCG where zbid = (select id from CProZBLS where dxid = '" + dxid + "')";
            var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBCG>(strsql);

            if (null != CProZBLS)
                CProZBLS.cgmx = CProZBCGmxs;
            //dynamic rest = new ExpandoObject();
            //rest.cgxx = CProZBLS;
            //rest.cgmx = CProZBCGmxs;
            return CProZBLS;
        }

        //public async static Task<dynamic> GetXMCGZBDLBydxid(string dxid)
        //{
        //    string strsql = "select * from View_ProZBCGLists where dxid = '" + dxid + "'";
        //    var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

        //    strsql = "select * from CProZBCG where zbid = (select id from CProZBLS where dxid = '" + dxid + "')";
        //    var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBCG>(strsql);

        //    if (null != CProZBLS)
        //        CProZBLS.cgmx = CProZBCGmxs;
        //    //dynamic rest = new ExpandoObject();
        //    //rest.cgxx = CProZBLS;
        //    //rest.cgmx = CProZBCGmxs;
        //    return CProZBLS;
        //}

        public async static Task<bool> DeleteCProCGXXMulti(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                for (int i = 0; i < ids.ids.Count; i++)
                {
                    string strsql = "delete from CProZBCG where zbid = '" + ids.ids[i].id + "'";
                    await irp.ExecuteSql(strsql);

                    strsql = "delete from CProZBLS where id = '" + ids.ids[i].id + "'";
                    await irp.ExecuteSql(strsql);

                }
             

                return true;

            });
        }

        public async static Task<bool> InsertCGXX(CProZBLS cgxx, List<CProZBCG> cgmxs,string ope)
        {
            if (ope == "add")
            {
                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //先删除
                    string strsql = "delete from CProZBCG where zbid = (select id from CProZBLS where dxid = '"+ cgxx.dxid + "')";
                    await irp.ExecuteSql(strsql);

                    strsql = "delete from CProZBLS where dxid = '" + cgxx.dxid + "'";
                    await irp.ExecuteSql(strsql);

                    //再新增
                    await irp.Insert(cgxx);

                    for (int i = 0; i < cgmxs.Count; i++)
                    {
                        cgmxs[i].zbid = cgxx.id;
                        await irp.Insert(cgmxs[i]);
                    }
                    

                    return true;

                });
            }
            else
            {
                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //再新增
                    await irp.Update(cgxx);

                    if (null!= cgmxs && cgmxs.Count > 0)
                    {
                        //先删除
                        string strsql = "delete from CProZBCG where zbid = '"+ cgxx.id + "'";
                        await irp.ExecuteSql(strsql);

                        //再添加明细
                        for (int i = 0; i < cgmxs.Count; i++)
                        {
                            cgmxs[i].zbid = cgxx.id;
                            await irp.Insert(cgmxs[i]);
                        }
                    }

                    return true;

                });
            }
        }

        public async static Task<int> InsertCGXXMX(CProZBCG cgmx,string ope)
        {
            if (ope == "add")
            {
                return await new RepositoryFactory().BaseRepository().Insert<CProZBCG>(cgmx);
            }
            else if (ope == "edit")
            {
                return await new RepositoryFactory().BaseRepository().Update<CProZBCG>(cgmx);
            }
            else if (ope == "del")
            {
                return await new RepositoryFactory().BaseRepository().Delete<CProZBCG>(cgmx);
            }
            else
                return 0;
        }
        #endregion

        #region 编标

        //没有编标
        public static async Task<(IEnumerable<dynamic>, int)> GetXZDXNoBBIndexList(ProBianbiaoQuerry querry, string strdepids)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.xmmc)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.cgmc)))
            {
                str = str + " and cgmc like '%" + querry.cgmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }
 

            if (!(string.IsNullOrEmpty(querry.xmfl)))
            {
                str = str + " and xmflname = '" + querry.xmfl + "'";
            }

            str += " and tlx.id not in (select dxid from dbo.CProZBBianB where ISNULL(lx,'cq') = 'cq' )";
 

            string sql = @"select tlx.*,xmmc,BuildUnit,xmflname from ( 
select* from(
select id,xmid,pid,dxmc,status,remark,lxje,dxbm, 
(select count(*) from CProManageLX where pid = a.id)cc
,(select dxmc from CProManageLX where id = a.pid)dxlx
 from CProManageLX a
)t where cc = 0 and pid<> '0'
)tlx left join CProManage pro ON tlx.xmid = pro.id where 1=1 and departid in " + strdepids + str.Replace("''", "'");//await UserRule.GetUserDepid(uid)

            sql = @"select tlx.*,xmmc,BuildUnit,xmflname,cgmc,cgbm,cgje,cglb,TotalMoney,projectmanager,xmnf  from ( 
select* from(
select id, xmid, pid, dxmc, status, remark, lxje, dxbm,
(select count(*) from CProManageLX where pid = a.id)cc
,(select dxmc from CProManageLX where id = a.pid)dxlx
 from CProManageLX a
)t where cc = 0 and pid<> '0' and status = '2'
)tlx
left join CProManage pro ON tlx.xmid = pro.id
left join(select id cgid, dxid, cgmc, cgbm, cglb, cgje from CProZBLS)tcg on tlx.id = tcg.dxid
 where 1 = 1 and (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");


            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public static async Task<(IEnumerable<dynamic>, int)> GetBianbiaoIndexList(ProBianbiaoQuerry querry, string strdepids)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and BuildUnit like '%" + querry.jsdw + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.xmmc)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.cgmc)))
            {
                str = str + " and cgmc like '%" + querry.cgmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like '%" + querry.dxmc + "%'";
            }
            //if (!(string.IsNullOrEmpty(querry.cgfs)))
            //{
            //    str = str + " and cgfs like '%" + querry.cgfs + "%'";
            //}
            if (!(string.IsNullOrEmpty(querry.cglb)))
            {
                str = str + " and cglb like '%" + querry.cglb + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmfl)))
            {
                str = str + " and xmflname = '" + querry.xmfl + "'";
            }

            //if (!(string.IsNullOrEmpty(querry.Begindate.ToString())))
            //{
            //    str = str + " and jhkssj >= ''" + querry.Begindate + "'' ";
            //}

            //if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
            //{
            //    str = str + " and jhkssj <= ''" + querry.Enddate + "''";
            //}

            if (!(string.IsNullOrEmpty(querry.bbzt)))
            {
                str = str + " and bbzt = ''" + querry.bbzt + "''";
            }

            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = ''" + querry.status + "''";
            }
            if (!(string.IsNullOrEmpty(querry.shzt)))
            {
                str = str + " and spstatus = ''" + querry.shzt + "''";
            }
            

            string sql = "select * from View_ProZBBBLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");//await UserRule.GetUserDepid(uid)
            bool isAsc = false;
            if (querry.sort == "Asc" || querry.sort == "asc")
            {
                isAsc = true;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        public static async Task<dynamic> GetBDById(string dxid)
        {
            //xmmc,dxmc,BuildUnit,tzxz,lxje
            string sql = "select * from view_ProDXLists where id = '" + dxid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<dynamic> GetBDByBBId(string bbid)
        {
            string sql = "select * from view_ProDXLists where id in (select dxid from CProZBBianB where id = '" + bbid + "')";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<dynamic> GetCGXXById(string dxid)
        {
            string sql = "";
            sql = "select * from View_ProZBCGLists where dxid = '" + dxid + "'";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public static async Task<dynamic> GetCGXXByBBId(string bbid)
        {
            //cgmc,cgje,cglb
            string sql = "";
            sql = "select * from View_ProZBCGLists where dxid in (select dxid from CProZBBianB where id = '" + bbid + "')";
            return await new RepositoryFactory().BaseRepository().FindEntity(sql, null);
        }

        public async static Task<dynamic> GetXMBBEntity(string bbid)
        {
            string strsql = "select * from View_ProZBBBLists where id = '" + bbid + "'";
            var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

            strsql = "select * from CProZBBianBMX where bbid = '" + bbid + "'";
            var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBBianBMX>(strsql);

            if (null != CProZBLS)
                CProZBLS.bbmx = CProZBCGmxs;

            //dynamic rest = new ExpandoObject();
            //rest.cgxx = CProZBLS;
            //rest.cgmx = CProZBCGmxs;
            return CProZBLS;
        }

        public async static Task<dynamic> GetXMBBEntityByDxid(string dxid,string strlx)
        {
            string strsql = "select * from View_ProZBBBLists where dxid = '" + dxid + "' ";
            var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

            strsql = "select * from CProZBBianBMX where bbid = (select id from CProZBBianB where dxid = '" + dxid + "' and isnull(lx,'cq') = '"+ strlx + "')";
            var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBBianBMX>(strsql);

            if (null != CProZBLS)
                CProZBLS.bbmx = CProZBCGmxs;
            //dynamic rest = new ExpandoObject();
            //rest.cgxx = CProZBLS;
            //rest.cgmx = CProZBCGmxs;
            return CProZBLS;
        }

        public async static Task<dynamic> GetXMBBEzzByDxid(string dxid)
        {
            string strsql = "select * from View_ProZBBBListsZZ where dxid = '" + dxid + "' ";
            var CProZBLS = await new RepositoryFactory().BaseRepository().FindEntity(strsql);

            strsql = "select * from CProZBBianBMX where bbid = (select id from CProZBBianB where dxid = '" + dxid + "' and isnull(lx,'cq') = 'zz')";
            var CProZBCGmxs = await new RepositoryFactory().BaseRepository().FindList<CProZBBianBMX>(strsql);

            if (null != CProZBLS)
                CProZBLS.bbmx = CProZBCGmxs;
    
            return CProZBLS;
        }

        public async static Task<bool> DeleteCProBBXXMulti(Root ids)
        {
            return await TranstionRule.GetRunActionTransAsync(async irp =>
            {
                for (int i = 0; i < ids.ids.Count; i++)
                {
                    string strsql = "delete from CProZBBianBMX where bbid = '" + ids.ids[i].id + "'";
                    await irp.ExecuteSql(strsql);

                    strsql = "delete from CProZBBianB where id = '" + ids.ids[i].id + "'";
                    await irp.ExecuteSql(strsql);

                }


                return true;

            });
        }

        public async static Task<bool> InsertBBXX(CProZBBianB bbxx, List<CProZBBianBMX> bbmxs, string ope,string strlx)
        {
            if (ope == "add")
            {
                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //先删除
                    string strsql = "delete from CProZBBianBMX where bbid = (select id from CProZBBianB where dxid = '" + bbxx.dxid + "' and isnull(lx,'cq') = '"+ strlx + "')";
                    await irp.ExecuteSql(strsql);

                    strsql = "delete from CProZBBianB where dxid = '" + bbxx.dxid + "' and isnull(lx,'cq') = '" + strlx + "'";
                    await irp.ExecuteSql(strsql);

                    //再新增
                    await irp.Insert(bbxx);

                    for (int i = 0; i < bbmxs.Count; i++)
                    {
                        bbmxs[i].id = CommonHelper.GetUniqueId().ToString();

                        await irp.Insert(bbmxs[i]);
                    }


                    return true;

                });
            }
            else
            {
                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    //再新增
                    await irp.Update(bbxx);

                    if (null != bbmxs && bbmxs.Count > 0)
                    {
                        //先删除
                        string strsql = "delete from CProZBBianBMX where bbid = (select id from CProZBBianB where dxid = '" + bbxx.dxid + "' and isnull(lx,'cq') = '" + strlx + "')";
                        await irp.ExecuteSql(strsql);

                        //再添加明细
                        for (int i = 0; i < bbmxs.Count; i++)
                        {
                            bbmxs[i].id = CommonHelper.GetUniqueId().ToString();

                            bbmxs[i].bbid = bbxx.id;
                            await irp.Insert(bbmxs[i]);
                        }
                    }

                    return true;

                });
            }
        }

        #endregion

    }
}
